package ac.github.os.internal.core.action.entity

import ac.github.os.internal.core.action.IEntityAction
import ac.github.os.internal.core.config.StringConfig
import ac.github.os.internal.core.config.get
import ac.github.os.internal.session.Cache
import ac.github.os.internal.session.Session
import taboolib.common.platform.Awake
import taboolib.library.xseries.XPotion

/**
 * duration d
 * amplifier a
 */
@Awake
class PotionAction : IEntityAction {
    override fun onExecute(session: Session, cache: Cache, config: StringConfig) {
        val xPotion = config.get<XPotion>("type", single = true) ?: return
        val duration = config.get<Int>("d") ?: return
        val amplifier = config.get<Int>("a") ?: return
        val cover = config.get("cover", false)!!
        xPotion.parsePotion(duration, amplifier)?.apply {
            cache.filterIsEntities().forEach {
                if (cover && it.hasPotionEffect(this.type)) {
                    it.removePotionEffect(this.type)
                }
                it.addPotionEffect(this)
            }
        }
    }

    override val namespace: String
        get() = "entity:potion"
}